﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class AnnounceController : SecureController
    {
        private ICommonDao<Announce> _AnnounceDao;

        public AnnounceController(ICommonDao<Announce> announceDao)
        {
            _AnnounceDao = announceDao;
        }


        public ActionResult Index()
        {
            var q = _AnnounceDao.GetAll();

            return View(q);
        }

        public ActionResult Create()
        {
            var q = new Announce();
            return View(q);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Announce item)
        {
            if (ModelState.IsValid)
            {
                _AnnounceDao.Save(item);
                _AnnounceDao.SaveChanges();
                return RedirectToAction("index");
            }
            else
            {

                return View(item);
            }
        }


        public ActionResult Edit(int id)
        {
            var q = _AnnounceDao.GetById(id);
            return View(q);
        }
        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(Announce item, int id)
        {
            if (ModelState.IsValid)
            {
                Announce ac = _AnnounceDao.GetById(id);
                UpdateModel<Announce>(ac, new string[] { "Title", "Content", "Created", "Sort" });
                _AnnounceDao.SaveChanges();
                return RedirectToAction("index");
            }

            return View(item);
        }

        public ActionResult Delete(int id)
        {
            var q = _AnnounceDao.GetById(id);
            _AnnounceDao.Delete(q);
            _AnnounceDao.SaveChanges();
            return RedirectToAction("index");
        }
    }
}